<?php

class Application_Model_DbTable_Article extends Zend_Db_Table_Abstract
{

    protected $_name = 'articles';
    
    protected $_referenceMap = array(
        "Category" => array(
            'columns' => "categories_id",
            'refTableClass' => "Application_Model_DbTable_ArticleCategory",
            'refColumns'=>"idcategories"
        ),
        
        "User" => array(
            'columns' => "user_id",
            'refTableClass' => "Application_Model_DbTable_User",
            'refColumns'=>"idusers"
        )
    );

    public function fetchPaginatorAdapter($filters=array(), $sortField = null, $sortType="asc")
    {
        $select = $this->select();
        
        //filter
        if(count($filters)>0)
        {
            foreach($filters as $field=>$filter)
            {
                if(is_string($filter))
                {
                    $select->where("$field LIKE '%$filter%'");
                }
                else
                {
                    $select->where("$field=?", $filter);
                }
            }
        }
        
        //sort
        if(null != $sortField)
        {
            $select->order($sortField." ".$sortType);
        }
        
        $adapter = new Zend_Paginator_Adapter_DbTableSelect($select);
        return $adapter;
    }

}

